package com.rongshu.view.userInfo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.utils.RxHelper;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.login.activity.InputAuthCodeActivity;
import com.umeng.socialize.UMShareAPI;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import rx.Subscriber;

public class BindPhoneActivity extends BaseActivity {
    @BindView(R.id.txt_top_title)
    TextView txt_top_title;
    @BindView(R.id.edtNewsPassword)
    EditText edtNewsPassword;
    @BindView(R.id.txtFinishLogin)
    TextView txtFinishLogin;

    //intent
    private Intent intent = null;
    //type
    private int type = 0;
    //times
    private int currentTimes = 60;
    //request
    private final int REQUEST_CODE = 0x01;
    //code
    private String code = null;
    //是否需要修改昵称
    private int is_show_nicheng = 1;
    //是否显示标签页
    private int is_show_biaoqian = 1;
    //是否第三方登录进入
    private boolean isFromThird = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        immersionBar.keyboardEnable(true, WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN
                | WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);  //软键盘自动弹出
        immersionBar.init();
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_bind_phone_view;
    }

    @Override
    protected void initView() {
        type = getIntent().getIntExtra(Constant.INTENT_TYPE, Constant.BIND_PHONE);
        is_show_nicheng = getIntent().getIntExtra(Constant.INTENT_DATA, is_show_nicheng);
        is_show_biaoqian = getIntent().getIntExtra(Constant.INTENT_MESSAGE, is_show_biaoqian);
        isFromThird = getIntent().getBooleanExtra(Constant.INTENT_THIRD, isFromThird);
        txt_top_title.setText("绑定手机号码");
        txtFinishLogin.setClickable(false);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnTextChanged(value = R.id.edtNewsPassword, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void pwdTextChanged(Editable editable) {
        txtFinishLogin.setBackgroundResource(getBtnState() ?
                R.drawable.shape_btn_3cc051 : R.drawable.shape_btn_8fe49d);
        txtFinishLogin.setClickable(!TextUtils.isEmpty(editable.toString()));
    }

    @OnClick({R.id.layout_back, R.id.txtFinishLogin})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                onBackPressed();
                break;
            case R.id.txtFinishLogin:
                intent = new Intent(this, InputAuthCodeActivity.class);
                intent.putExtra(Constant.INTENT_DATA, edtNewsPassword.getText().toString());
                intent.putExtra(Constant.INTENT_TYPE, type);
                intent.putExtra(Constant.INTENT_MESSAGE, currentTimes);
                intent.putExtra(Constant.INTENT_CODE, code);
                startActivityForResult(intent, REQUEST_CODE);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        showKeyboard(false);
        if (isFromThird) {
            setResult(RESULT_OK);
        }
        finish();
    }

    private boolean getBtnState() {
        return edtNewsPassword.getText().toString().length() == 11 && !TextUtils.isEmpty(edtNewsPassword.getText().toString());
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE) {
                if (data != null && data.hasExtra(Constant.INTENT_MESSAGE)) {
                    currentTimes = data.getIntExtra(Constant.INTENT_MESSAGE, 0);
                    code = data.getStringExtra(Constant.INTENT_CODE);
                    cutDownTime();
                } else if (data != null && data.hasExtra(Constant.INTENT_DATA)) {
                    Intent intent = new Intent();
                    intent.putExtra(Constant.INTENT_DATA, data.getStringExtra(Constant.INTENT_DATA));
                    setResult(RESULT_OK, intent);
                }
                finish();
            }
        }
    }

    //倒计时操作
    private void cutDownTime() {
        RxHelper.countdown(currentTimes)
                .subscribe(new Subscriber<Integer>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Integer integer) {
                        currentTimes = integer;
                    }
                });
    }
}
